;  This file is part of tornado.
; 
;    tornado is free software: you can redistribute it And/Or modify
;    it under the terms of the GNU General Public License As published by
;    the Free Software Foundation, either version 3 of the License, Or
;    any later version.
; 
;    tornado is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;    GNU General Public License For more details.
; 
;    You should have received a copy of the GNU General Public License
;    along With tornado.  If Not, see <http://www.gnu.org/licenses/>. 2
; 

Declare.a getPacketType(*p.sPacket)

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
ProcedureDLL serverAddPacket(*p.sPacket, *s.sTornadoServer)
  If *p  
    Select getPacketType(*p)
      Case #PACKET_SYSTEM
        AddElement(*s\mPacketManager\mSystemPacket())
        *s\mPacketManager\mSystemPacket() = *p
      Case #PACKET_CUSTOM
        AddElement(*s\mPacketManager\mCustomPacket())
        *s\mPacketManager\mCustomPacket() = *p
    EndSelect
  EndIf 
EndProcedure

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
ProcedureDLL serverSendPacketNow(*p.sPacket, *s.sTornadoServer)
  If *p<>#Null And *s<>#Null 
    
    destination.i = *p\mDestination
    If destination <> -1
      SendNetworkData(destination,*p\mData,#PACKET_LIMIT)
    Else
      For i = 0 To *s\mMaxPlayer - 1
        If *s\mClientSlot(i)\mClientID <> 0
          SendNetworkData(*s\mClientSlot(i)\mClientID,*p\mData,#PACKET_LIMIT)
        EndIf 
      Next
    EndIf

    FreeMemory(*p\mData)
    FreeMemory(*p)
  EndIf 
EndProcedure


; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
ProcedureDLL clientAddPacket(*p.sPacket, *c.sTornadoClient)
  If *p  
    
    ;
    ; add from who at the end
    ;
    PokeL(*p\mData + #PACKET_LIMIT - 4, *c\mSlot)

    Select getPacketType(*p)
      Case #PACKET_SYSTEM
        AddElement(*c\mPacketManager\mSystemPacket())
        *c\mPacketManager\mSystemPacket() = *p
      Case #PACKET_CUSTOM
        AddElement(*c\mPacketManager\mCustomPacket())
        *c\mPacketManager\mCustomPacket() = *p
    EndSelect
  EndIf 
EndProcedure

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
ProcedureDLL clientSendPacketNow(*p.sPacket, *c.sTornadoClient)
    If *p<>#Null And *c<>#Null 
      ;
      ; add from who at the end
      ;
      PokeL(*p\mData + #PACKET_LIMIT - 4, *c\mSlot)
      SendNetworkData(*c\mClientID,*p\mData,#PACKET_LIMIT)
      
      FreeMemory(*p\mData)
      FreeMemory(*p)
    EndIf 
EndProcedure
